package com.wechart.service.manage.impl;


import com.wechart.dao.domain.Orgroundinfo;
import com.wechart.dao.domain.OrgroundinfoCriteria;
import com.wechart.dao.mapper.OrgroundinfoMapper;
import com.wechart.model.ManagerModel;
import com.wechart.service.manage.OrgroundinfoService;
import com.wechart.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service(value = "orgroundinfoService")
public class OrgroundinfoServiceImpl implements OrgroundinfoService {

    @Autowired
    private OrgroundinfoMapper orgroundinfoMapper;

    @Override
    public List<Orgroundinfo> getOrgroundinfo(ManagerModel managerModel){
        OrgroundinfoCriteria orgroundinfoCriteria=new OrgroundinfoCriteria();
        OrgroundinfoCriteria.Criteria criteria=orgroundinfoCriteria.createCriteria();
        if(StringUtils.isNoNullString(StringUtils.getIdFromString(managerModel.getOrgcode()))){
            criteria.andOrgcodeEqualTo(StringUtils.getIdFromString(managerModel.getOrgcode()));
        }
        if(StringUtils.isNoNullString(managerModel.getTrncode())){
            criteria.andTrncodeEqualTo(managerModel.getTrncode());
        }
        if (managerModel.getOrgcodelist().size()>0 && !managerModel.getAreacode().equals("3300")){
            criteria.andOrgcodeIn(managerModel.getOrgcodelist());
        }
        orgroundinfoCriteria.setOrderByClause("trncode asc");
        return orgroundinfoMapper.selectByExample(orgroundinfoCriteria);
    }
    /**
     * 机构轧差场次信息表插入
     * @param managerModel
     * @return
     */
    @Override
    public int insertOrgroundinfo(ManagerModel managerModel){
        OrgroundinfoCriteria orgroundinfoCriteria=new OrgroundinfoCriteria();
        Orgroundinfo data = new Orgroundinfo();
//        data.setOrgcode(managerModel.getOrgcode());
        data.setTrncode(managerModel.getTrncode());
        data.setNetround(Short.parseShort(managerModel.getNetround()));
        data.setOrground(Short.parseShort(managerModel.getOrground()));
        data.setNextflag(Short.parseShort(managerModel.getNextflag()));
        return orgroundinfoMapper.insertSelective(data);
    }

    /**
     * 机构轧差场次信息表修改
     * @param managerModel
     * @return
     */
    @Override
    public int modifyOrgroundinfo(ManagerModel managerModel ,String id) {
        OrgroundinfoCriteria orgroundinfoCriteria=new OrgroundinfoCriteria();
        orgroundinfoCriteria.createCriteria().andOrgcodeEqualTo(id);
        Orgroundinfo data = new Orgroundinfo();
//        data.setOrgcode(managerModel.getOrgcode());
        data.setTrncode(managerModel.getTrncode());
        data.setNetround(Short.parseShort(managerModel.getNetround()));
        data.setOrground(Short.parseShort(managerModel.getOrground()));
        data.setNextflag(Short.parseShort(managerModel.getNextflag()));
        return orgroundinfoMapper.updateByExampleSelective(data,orgroundinfoCriteria);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Orgroundinfo getOrgroundinfoById(String  id) {
        OrgroundinfoCriteria orgroundinfoCriteria=new OrgroundinfoCriteria();
        orgroundinfoCriteria.createCriteria().andOrgcodeEqualTo(id);
        List<Orgroundinfo> data = orgroundinfoMapper.selectByExample(orgroundinfoCriteria);
        if (null!=data) {
            return data.get(0);
        }else{
            return null;
        }
    }
}
